﻿KISSY.add("fgt/auth", function (S, LoginDialog) {
    var auth = {
        getLoginDialog: function () {
            if (S.isUndefined(FGT.globalObj.loginDlg)) {
                FGT.globalObj.loginDlg = new LoginDialog();
            }
            return FGT.globalObj.loginDlg;
        },

        validate: function (passCallBack, popbox, context, successCallbackParam) {
            var self = this,
                resultCode = FGT.core.constant.jsonResultCode,
                loginDialog;

            jQuery.ajax({
                url: "/account/validate",
                type: "GET",
                cache: false,
                success: function (data, textStatus, jqXHR) {
                    popbox.hide();
                    if (data.Result === resultCode.SUCCESS) {
                        passCallBack.apply(context || self, successCallbackParam || []);
                    }
                    else {
                        loginDialog = self.getLoginDialog();
                        loginDialog.show(passCallBack, undefined, context, successCallbackParam);
                    }

                },
                error: function (jqXHR, textStatus, errorThrown) {
                    popbox.show("Internal error, please try again later", 1000);
                }
            });
        }

    };
    S.FGT.auth = auth;

    return auth;
}, { requires: ["./auth/loginDialog"] });